package com.jing.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jing.consumer.service.TestService;
import com.jing.domain.User;
import com.jing.redis.JedisClient;

@Controller
@RequestMapping("/test")
public class TestController {
	@Resource
	TestService testService;
	
	@Resource
	JedisClient jedisClient;
	
	@RequestMapping("/hello")
	@ResponseBody
    public String hello() {
		testService.test();
		return "ok";
	}
	@RequestMapping("/save")
	@ResponseBody
	public Integer save() {
		User user = new User();
		user.setUsername("赵云");
		user.setPassword("123");
		user.setAddress("sasasas");
		return testService.save(user);
	}
	@RequestMapping("/getById")
	@ResponseBody
	public User getById(String id) {
		id = "1";
		User user = (User) jedisClient.getObject(id);
		if (user == null) {
			// 如果Redis没有则从数据库里取
			user = testService.get(Integer.valueOf(id));
			if (user != null) {
				// 从数据库中取了之后，再存入Redis，以便下次取
				jedisClient.setObject(id, (Object) user);
			}
		}
		return user;
	}
}
